
# 本cmake文件，供三方引入CGraph引用，用于屏蔽系统和C++版本的区别

# 根据当前 CGraph-env-include.cmake 文件的位置，定位CGraph整体工程的路径
# 从而解决并兼容了直接引入和三方库引入的路径不匹配问题
get_filename_component(CGRAPH_PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)
set(CGRAPH_PROJECT_ROOT_DIR "${CGRAPH_PROJECT_CMAKE_DIR}/../")
file(GLOB_RECURSE CGRAPH_PROJECT_SRC_LIST "${CGRAPH_PROJECT_ROOT_DIR}/src/*.cpp")

# 检查是否处于交叉编译环境
if(CMAKE_CROSSCOMPILING)
    message(STATUS "[CGraph] Cross compiling for ${CMAKE_SYSTEM_NAME}")
    message(STATUS "[CGraph] Target architecture: ${CMAKE_SYSTEM_PROCESSOR}")
    
    # 设置通用交叉编译选项
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wno-deprecated-declarations -pthread")
    add_definitions(-D_ENABLE_LIKELY_)
    
    # 根据不同架构设置特定优化
    if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
        message(STATUS "[CGraph] Applying AArch64 specific optimizations")
        # 检查是否为htnice平台
        if(CMAKE_CXX_COMPILER MATCHES ".*htnice.*")
            message(STATUS "[CGraph] Applying htnice specific optimizations")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crc")
            # 添加htnice平台特定的编译选项
            # 如果需要，可以在这里添加更多htnice特定的编译标志
        else()
            # 其他aarch64平台
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crc -mtune=cortex-a53")
        endif()
    elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm")
        # 检查编译器是否为htnice平台
        if(CMAKE_CXX_COMPILER MATCHES ".*htnice.*")
            message(STATUS "[CGraph] Applying ARM htnice specific optimizations")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4")
            # 添加htnice平台特定的编译选项
            # 如果需要，可以在这里添加更多htnice特定的编译标志
        # 检查编译器是否为Hisilicon编译器
        elseif(CMAKE_CXX_COMPILER MATCHES ".*hisiv500.*")
            message(STATUS "[CGraph] Applying Hisilicon Hi35xx specific optimizations")
            # 添加Hisilicon平台特定的编译选项
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -mtune=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4")
            # 如果需要，添加Hisilicon SDK中的头文件路径
            # include_directories(/path/to/hisiv500/sdk/include)
            # link_directories(/path/to/hisiv500/sdk/lib)
        else()
            # 其他ARM平台的通用优化
            message(STATUS "[CGraph] Applying generic ARM optimizations")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4")
        endif()
    endif()

ELSE()
    # 原有的平台检测逻辑
    IF(APPLE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -O2 \
            -finline-functions -Wno-deprecated-declarations -Wno-c++17-extensions")
        add_definitions(-D_ENABLE_LIKELY_)
    ELSEIF(UNIX)
        # linux平台，加入多线程内容
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread -Wno-format-overflow")
        add_definitions(-D_ENABLE_LIKELY_)
    ELSEIF(WIN32)
        IF(MSVC)
            # windows平台，加入utf-8设置。否则无法通过编译
            add_definitions(/utf-8)
            add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
            add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

            # 禁止几处warning级别提示
            add_compile_options(/wd4996)
            add_compile_options(/wd4267)
            add_compile_options(/wd4018)
        ENDIF()
        # 本工程也支持在windows平台上的mingw环境使用
    ENDIF()
ENDIF()

include_directories(${CGRAPH_PROJECT_ROOT_DIR}/src/)    # 直接加入"CGraph.h"文件对应的位置

# 交叉编译时，推荐使用STATIC库类型
if(CMAKE_CROSSCOMPILING)
    message(STATUS "[CGraph] Building static library for cross compilation")
    add_library(CGraph STATIC ${CGRAPH_PROJECT_SRC_LIST})    # 编译libCGraph静态库
else()
    # 以下三选一，本地编译执行，推荐OBJECT方式
    add_library(CGraph OBJECT ${CGRAPH_PROJECT_SRC_LIST})      # 通过代码编译
    # add_library(CGraph SHARED ${CGRAPH_PROJECT_SRC_LIST})    # 编译libCGraph动态库
    # add_library(CGraph STATIC ${CGRAPH_PROJECT_SRC_LIST})    # 编译libCGraph静态库
endif()
